<template>
  <div class="data-aggregation-container">
    <div class="dashboard-header">
      <div class="header-left">
        <h1><i class="fas fa-database"></i> 数据接入监控</h1>
        <p>实时监控数据接入状态与异常情况</p>
      </div>
      <div class="header-right">
        <div class="time-display">
          {{ currentTime }}
        </div>
        <el-button
            class="alert-btn"
            @click="showAlertPanel = true"
            icon="el-icon-warning-outline">
          告警 ({{ alertCount }})
        </el-button>
      </div>
    </div>

    <div class="chart-container">
      <!-- 缩小后的统计卡片 -->
      <div class="compact-stats">
        <div class="stat-card online">
          <div class="card-header">
            <div class="card-icon">
              <i class="fas fa-plug"></i>
            </div>
            <div>
              <div class="card-title">在线接入量</div>
              <div class="stat-value">1,248</div>
            </div>
          </div>
          <div class="stat-trend">
            <i class="fas fa-arrow-up trend-up"></i>
            <span class="trend-up">12.5%</span>
          </div>
        </div>

        <div class="stat-card offline">
          <div class="card-header">
            <div class="card-icon">
              <i class="fas fa-unlink"></i>
            </div>
            <div>
              <div class="card-title">离线接入量</div>
              <div class="stat-value">42</div>
            </div>
          </div>
          <div class="stat-trend">
            <i class="fas fa-arrow-down trend-down"></i>
            <span class="trend-down">3.8%</span>
          </div>
        </div>

        <div class="stat-card">
          <div class="card-header">
            <div class="card-icon" style="background: rgba(0, 150, 255, 0.15); color: #0096ff;">
              <i class="fas fa-bolt"></i>
            </div>
            <div>
              <div class="card-title">数据处理量</div>
              <div class="stat-value">2.4M</div>
            </div>
          </div>
          <div class="stat-trend">
            <i class="fas fa-arrow-up trend-up"></i>
            <span class="trend-up">8.2%</span>
          </div>
        </div>

        <div class="stat-card">
          <div class="card-header">
            <div class="card-icon" style="background: rgba(150, 100, 255, 0.15); color: #9664ff;">
              <i class="fas fa-tachometer-alt"></i>
            </div>
            <div>
              <div class="card-title">平均延迟</div>
              <div class="stat-value">86<span>ms</span></div>
            </div>
          </div>
          <div class="stat-trend">
            <i class="fas fa-arrow-down trend-up"></i>
            <span class="trend-up">5.3%</span>
          </div>
        </div>
      </div>

      <!-- 图表并行布局 -->
      <div class="chart-row">
        <RealTimeDashboard />
        <DataSourceMonitor />
      </div>
    </div>

    <!-- 告警弹窗 -->
    <el-dialog
        title="异常数据告警"
        v-model="showAlertPanel"
        width="50vw"
        top="5vh">
      <AlertPanel />
    </el-dialog>
  </div>
</template>

<script>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import RealTimeDashboard from './monitor/RealTimeDashboard.vue';
import DataSourceMonitor from './monitor/DataSourceMonitor.vue';
import AlertPanel from './monitor/AlertPanel.vue';
import {ElButton, ElDialog} from "element-plus";

export default {
  name: 'DataAggregationMonitor',
  components: {
    ElButton,
    ElDialog,
    RealTimeDashboard,
    DataSourceMonitor,
    AlertPanel
  },
  setup() {
    const currentTime = ref('');
    const showAlertPanel = ref(false);
    const alertCount = ref(5); // 告警数量
    let timer = null;

    const updateTime = () => {
      const now = new Date();
      currentTime.value = now.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false
      });
    };

    onMounted(() => {
      updateTime();
      timer = setInterval(updateTime, 1000);
    });

    onBeforeUnmount(() => {
      if (timer) clearInterval(timer);
    });

    return {
      currentTime,
      showAlertPanel,
      alertCount
    };
  }
};
</script>

<style scoped>
.data-aggregation-container {
  width: 96vw;
  background-color: #0f1c3c;
  color: #e4e8f0;
  overflow-x: auto;
  height: 100vh;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2vh;
  padding: 1.5vh 2vw;
  background: linear-gradient(90deg, #1a2a52, #0a1a3a);
  border-radius: 1.2vw;
  box-shadow: 0 0.4vh 2vh rgba(0, 0, 0, 0.25);
  border: 0.1vw solid #2a3a6a;
}

.header-left h1 {
  font-size: 1.2vw;
  font-weight: 600;
  background: linear-gradient(to right, #4facfe, #00f2fe);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 0.5vh;
}

.header-left p {
  font-size: 0.8vw;
  color: #a0b1d0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 1vw;
}

.time-display {
  font-size: 1vw;
  font-weight: 500;
  color: #4facfe;
  background: rgba(10, 25, 55, 0.7);
  padding: 0.8vh 1.5vw;
  border-radius: 3vw;
  border: 0.1vw solid #2a4a7a;
}

.alert-btn {
  background: linear-gradient(135deg, #ff5a5a, #cc4848);
  color: white;
  border: none;
  padding: 0.8vh 1vw;
  font-size: 1vw;
  border-radius: 0.8vw;
  cursor: pointer;
  transition: all 0.3s;
}

.alert-btn:hover {
  transform: translateY(-0.2vh);
  box-shadow: 0 0.4vh 1.2vw rgba(255, 90, 90, 0.6);
}

.chart-container {
  display: flex;
  flex-direction: column;
  gap: 1.5vh;
  margin-bottom: 2.5vh;
}

/* 统计卡片 */
.compact-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(18vw, 1fr));
  gap: 1vw;
  margin-bottom: 1.5vh;
  box-sizing: content-box;
}

.stat-card {
  background: linear-gradient(135deg, #1a2a52, #152245);
  border-radius: 1.2vw;
  padding: 1.2vh 1vw;
  box-shadow: 0 0.6vh 1.6vw rgba(0, 0, 0, 0.3);
  border: 0.1vw solid #2a3a6a;
  transition: transform 0.3s ease;
  display: flex;
  flex-direction: column;
  height: 10vh;
  box-sizing: content-box;
}

.stat-card:hover {
  transform: translateY(-0.5vh);
  box-shadow: 0 0.8vh 2vh rgba(0, 0, 0, 0.4);
}

.card-header {
  display: flex;
  align-items: center;
  margin-bottom: 1vh;
}

.card-icon {
  width: 2.5vw;
  height: 2.5vw;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  margin-right: 1.5vw;
  font-size: 1.2vw;
}

.online .card-icon {
  background: rgba(0, 210, 150, 0.15);
  color: #00d296;
}

.offline .card-icon {
  background: rgba(255, 90, 90, 0.15);
  color: #ff5a5a;
}

.card-title {
  font-size: 1vw;
  font-weight: 500;
  color: #c0d0f0;
  margin-bottom: 0.5vh;
}

.stat-value {
  font-size: 1.2vw;
  font-weight: 600;
  margin-top: auto;
}

.stat-trend {
  display: flex;
  align-items: center;
  color: #8898b8;
  font-size: 1vw;
  margin-top: 0.5vh;
}

.trend-up {
  color: #00d296;
}

.trend-down {
  color: #ff5a5a;
}

/* 图表并行布局 */
.chart-row {
  display: flex;
  gap: 1.5vw;
}

.chart-row > * {
  flex: 1;
  min-width: 0;
}

@media (max-width: 768px) {
  .header-left h1 {
    font-size: 3.5vw;
  }

  .header-left p,
  .time-display,
  .alert-btn {
    font-size: 2.2vw;
  }

  .card-title {
    font-size: 2.2vw;
  }

  .stat-value {
    font-size: 3.2vw;
  }

  .stat-trend {
    font-size: 1.8vw;
  }

  .compact-stats {
    grid-template-columns: repeat(auto-fit, minmax(40vw, 1fr));
  }

  /* 移动端切换为垂直布局 */
  .chart-row {
    flex-direction: column;
    gap: 2vh;
  }
}
</style>
